package com.bkgy.controller.optimize.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bkgy.commom.model.CExportExcelView;
import com.bkgy.controller.optimize.dao.OptimizeDao;
import com.bkgy.controller.optimize.dto.Body;
import com.bkgy.controller.optimize.dto.HmiBaseRequest;
import com.bkgy.controller.optimize.dto.HmiBaseResponse;
import com.bkgy.controller.optimize.dto.OptimizeDistributeRes;
import com.bkgy.controller.optimize.entity.*;
import com.bkgy.controller.optimize.service.OptimizeService;
import com.bkgy.modular.httppost.HttpClientService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.DateUtil;
import com.bkgy.util.ExcelUtilsPOI;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.ui.ModelMap;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Author:Lipeng Zhao
 * Description:
 *
 * @Date:Created In 14:58 2025/5/6
 */
@Service
public class OptimizeServiceImpl implements OptimizeService {

    @Autowired
    private OptimizeDao optimizeDao;

    @Autowired
    private HttpClientService httpClientService;

    @Value("${hmi}")
    private String hmi;


    @Override
    public ResultData findMapByCondition(AcidRollingProductRequest request) {
        try {
            Page<?> page = new Page<>(request.getPageIndex(), request.getPageSize());
            List<ParamOptimizeEntity> list = optimizeDao.findMapByCondition(page, request);
            Integer count = optimizeDao.findCountByCondition(request);
            return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }

    @Override
    public ResultData findAcidRollingProduct(AcidRollingProductRequest request) {
        try {
            ParamOptimizeEntity paramOptimizeEntity = optimizeDao.findAcidRollingProduct(request);
            return ResultData.ResultDataSuccess(paramOptimizeEntity);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }

    @Override
    public ResultData findCaseMapByCondition(AcidRollingProductRequest request) {
        try {
            Page<?> page = new Page<>(request.getPageIndex(), request.getPageSize());
            List<CaseManageEntity> list = optimizeDao.findCaseMapByCondition(page, request);
            Integer count = optimizeDao.findCaseCountByCondition(request);
            return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }

    @Override
    public ResultData findLoadDistributionMapByCondition(LoadDistributionRequest request) {
        try {
            Page<?> page = new Page<>(request.getPageIndex(), request.getPageSize());
            List<LoadDistributionEntity> list = optimizeDao.findLoadDistributionMapByCondition(page, request);
            Integer count = optimizeDao.findLoadDistributionCountByCondition(request);
            return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }

    @Override
    public ResultData findLoadDistributionCurveData(LoadDistributionRequest request) {
        try {
            List<ParamOptimizeEntity> list = optimizeDao.findLoadDistributionCurveData(request);
            return ResultData.ResultDataSuccess(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }

    @Override
    public ResultData findCaseByCoilNo(AcidRollingProductRequest request) {
        try {
            CaseManageEntity caseManageEntity = optimizeDao.findCaseByCoilNo(request);
            return ResultData.ResultDataSuccess(caseManageEntity);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }

    @Override
    public ResultData deleteCaseByCoilNo(AcidRollingProductRequest request) {
        try {
            optimizeDao.deleteCaseByCoilNo(request);
            return ResultData.ResultDataSuccess("");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }

    @Override
    public ResultData updateCase(CaseManageEntity request) {
        try {
            AcidRollingProductRequest acidRollingProductRequest = new AcidRollingProductRequest();
            acidRollingProductRequest.setVolumeNum(request.getCoilNo());
            CaseManageEntity caseManageEntity = optimizeDao.findCaseByCoilNo(acidRollingProductRequest);
            request.setStatus(1);
            request.setRecCreateTime(new Date());
            if (caseManageEntity == null) {
                optimizeDao.insertCase(request);
            } else {
                optimizeDao.updateCase(request);
            }

            return ResultData.ResultDataSuccess("");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }


    @Override
    public ResultData importData(Map<String, Object> map) {
        MultipartFile file = (MultipartFile) map.get("file");
        StringBuffer error = new StringBuffer();


        try {
            XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());


            XSSFSheet sheet = workbook.getSheetAt(0);


            ResultData valParamMessage = validationParams(sheet);
            if (valParamMessage.getStatus().equals("2002")) {
                return ResultData.ResultDataFaultSelf(valParamMessage.getMsg(), "");
            }


            int rowNum = sheet.getLastRowNum();//行数
            for (int j = 1; j <= rowNum; j++) {
                XSSFRow row = sheet.getRow(j);
                int colNum = 9;
                ParamOptimizeEntity paramOptimizeEntity = new ParamOptimizeEntity();


                for (int k = 0; k < colNum; k++) {//列数
                    XSSFCell cell = row.getCell(k);
                    String val = null;
                    if (cell != null) {
                        row.getCell(k).setCellType(CellType.STRING);
                        val = cell.getStringCellValue();
                    }
                    switch (k) {
                        case 0://
                            paramOptimizeEntity.setCoilNo(val);
                            break;
                        case 1://
                            paramOptimizeEntity.setSteelGrade(val);
                            break;
                        case 2://
                            paramOptimizeEntity.setYp(BigDecimal.valueOf(Double.parseDouble(val)));
                            break;

                        case 3:
                            paramOptimizeEntity.setEnThick(BigDecimal.valueOf(Double.parseDouble(val)));
                            break;
                        case 4://
                            paramOptimizeEntity.setExThick(BigDecimal.valueOf(Double.parseDouble(val)));
                            break;
                        case 5://
                            paramOptimizeEntity.setExWidth(BigDecimal.valueOf(Double.parseDouble(val)));
                            break;
                        case 6://
                            paramOptimizeEntity.setL(BigDecimal.valueOf(Double.parseDouble(val)));
                            break;
                        case 7://
                            paramOptimizeEntity.setM(BigDecimal.valueOf(Double.parseDouble(val)));
                            break;
                        case 8://
                            paramOptimizeEntity.setN(BigDecimal.valueOf(Double.parseDouble(val)));
                            break;
                    }
                }

                AcidRollingProductRequest acidRollingProductRequest = new AcidRollingProductRequest();
                acidRollingProductRequest.setVolumeNum(paramOptimizeEntity.getCoilNo());
                CaseManageEntity caseManage = new CaseManageEntity();
                caseManage.setCoilNo(paramOptimizeEntity.getCoilNo());
                caseManage.setSteelGrade(paramOptimizeEntity.getSteelGrade());
                caseManage.setYp(paramOptimizeEntity.getYp());
                caseManage.setEnThick(paramOptimizeEntity.getEnThick());
                caseManage.setExThick(paramOptimizeEntity.getExThick());
                caseManage.setExWidth(paramOptimizeEntity.getExWidth());
                caseManage.setL(paramOptimizeEntity.getL());
                caseManage.setM(paramOptimizeEntity.getM());
                caseManage.setN(paramOptimizeEntity.getN());
                caseManage.setStatus(2);
                caseManage.setRecCreateTime(new Date());

                CaseManageEntity caseManageEntity = optimizeDao.findCaseByCoilNo(acidRollingProductRequest);
                if (caseManageEntity == null) {
                    optimizeDao.insertCase(caseManage);
                } else {
                    optimizeDao.updateCase(caseManage);
                }

                rollingSimulationCal(caseManage);
                Thread.sleep(5000);
            }
            return ResultData.ResultDataSuccess("导入成功");
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new RuntimeException(e.getMessage());
        }


    }

    private ResultData validationParams(XSSFSheet sheet) {
        StringBuffer error = new StringBuffer();

        // 1.校验必填项
        int rowNum = sheet.getLastRowNum();
        for (int j = 1; j < rowNum; j++) {
            XSSFRow row = sheet.getRow(j);
            int colNum = 9;
            if (row != null) {
                for (int k = 0; k <= colNum; k++) {
                    XSSFCell cell = row.getCell(k);
                    String val = null;

                    if (cell != null) {
                        row.getCell(k).setCellType(CellType.STRING);
                        val = cell.getStringCellValue();
                    }
                    switch (k) {
                        case 0:
                            if (StringUtils.isEmpty(val)) {
                                error.append("第" + (j + 1) + "行,任务名称不可为空;\n");
                                break;
                            }
                            break;
                        case 1:
                            if (StringUtils.isEmpty(val)) {
                                error.append("第" + (j + 1) + "行,产品牌号不可为空;\n");
                            }
                            break;
                        case 2:
                            if (StringUtils.isEmpty(val)) {
                                error.append("第" + (j + 1) + "行,屈服强度不可为空;\n");
                                break;
                            }
                            break;
                        case 3:
                            if (StringUtils.isEmpty(val)) {
                                error.append("第" + (j + 1) + "行,入口厚度不可为空;\n");
                                break;
                            }
                            break;
                        case 4:
                            if (StringUtils.isEmpty(val)) {
                                error.append("第" + (j + 1) + "行,成品厚度不可为空;\n");
                                break;
                            }
                            break;
                        case 5:
                            if (StringUtils.isEmpty(val)) {
                                error.append("第" + (j + 1) + "行,宽度不可为空;\n");
                            }
                            break;
                        case 6:
                            if (StringUtils.isEmpty(val)) {
                                error.append("第" + (j + 1) + "行,L不可为空;\n");
                            }
                            break;
                        case 7:
                            if (StringUtils.isEmpty(val)) {
                                error.append("第" + (j + 1) + "行,M不可为空;\n");
                            }
                            break;
                        case 8:
                            if (StringUtils.isEmpty(val)) {
                                error.append("第" + (j + 1) + "行,N不可为空;\n");
                            }
                            break;
                    }
                }

            }

        }
        if (error.length() > 0) {
            return ResultData.ResultDataFaultSelf(error.toString(), "");
        }
        return ResultData.ResultDataSuccess("");
    }

    public void rollingSimulationCal(CaseManageEntity caseManage) {


        Map map = new HashMap<String, String>();
        List<Map<String, String>> mapList = new ArrayList<>();
        HmiBaseRequest hmiBaseRequest = new HmiBaseRequest();
        map.put("CoilNo", caseManage.getCoilNo());
        map.put("SteelGrade", caseManage.getSteelGrade());
        map.put("YP", caseManage.getYp());
        map.put("res_EN_THICK", caseManage.getEnThick());
        map.put("res_EX_THICK", caseManage.getExThick());
        map.put("res_EX_WIDTH", caseManage.getExWidth());
        mapList.add(map);
        Body body = new Body();
        List<List<Map<String, String>>> list1 = new ArrayList<>();
        list1.add(mapList);
        body.setParams(list1);
        hmiBaseRequest.setBody(body);
        String json = JSON.toJSONString(hmiBaseRequest);

//        HmiBaseResponse hmiBaseResponse=new HmiBaseResponse();
//        List list=new ArrayList<>();
//        hmiBaseResponse.setResults(new ArrayList<>());
        String respStr = httpClientService.postJsonRequest(hmi, json, false, "");
        HmiBaseResponse responseDto = JSONObject.parseObject(respStr, HmiBaseResponse.class);
        if (responseDto.getCode() == 200) {
            if (responseDto.getResults().size() == 0) {
                return;
            }
            Object object = responseDto.getResults().get(0);
            JSONArray picArray = JSONArray.parseArray(object.toString());
            JSONObject obj = picArray.getJSONObject(0);
            caseManage.setDeformRate1(BigDecimal.valueOf(Double.parseDouble(obj.get("Deform_Rate1").toString())));
            caseManage.setDeformRate2(BigDecimal.valueOf(Double.parseDouble(obj.get("Deform_Rate2").toString())));
            caseManage.setDeformRate3(BigDecimal.valueOf(Double.parseDouble(obj.get("Deform_Rate3").toString())));
            caseManage.setDeformRate4(BigDecimal.valueOf(Double.parseDouble(obj.get("Deform_Rate4").toString())));
            caseManage.setDeformRate5(BigDecimal.valueOf(Double.parseDouble(obj.get("Deform_Rate5").toString())));
            caseManage.setWrDia1(BigDecimal.valueOf(Double.parseDouble(obj.get("Diameter1").toString())));
            caseManage.setWrDia2(BigDecimal.valueOf(Double.parseDouble(obj.get("Diameter2").toString())));
            caseManage.setWrDia3(BigDecimal.valueOf(Double.parseDouble(obj.get("Diameter3").toString())));
            caseManage.setWrDia4(BigDecimal.valueOf(Double.parseDouble(obj.get("Diameter4").toString())));
            caseManage.setWrDia5(BigDecimal.valueOf(Double.parseDouble(obj.get("Diameter5").toString())));

            caseManage.setNr1(BigDecimal.valueOf(Double.parseDouble(obj.get("Roll_Num1").toString())));
            caseManage.setNr2(BigDecimal.valueOf(Double.parseDouble(obj.get("Roll_Num2").toString())));
            caseManage.setNr3(BigDecimal.valueOf(Double.parseDouble(obj.get("Roll_Num3").toString())));
            caseManage.setNr4(BigDecimal.valueOf(Double.parseDouble(obj.get("Roll_Num4").toString())));
            caseManage.setNr5(BigDecimal.valueOf(Double.parseDouble(obj.get("Roll_Num5").toString())));

            caseManage.setUnitTension1(BigDecimal.valueOf(Double.parseDouble(obj.get("Unit_Tension1").toString())));
            caseManage.setUnitTension2(BigDecimal.valueOf(Double.parseDouble(obj.get("Unit_Tension2").toString())));
            caseManage.setUnitTension3(BigDecimal.valueOf(Double.parseDouble(obj.get("Unit_Tension3").toString())));
            caseManage.setUnitTension4(BigDecimal.valueOf(Double.parseDouble(obj.get("Unit_Tension4").toString())));
            caseManage.setUnitTension5(BigDecimal.valueOf(Double.parseDouble(obj.get("Unit_Tension5").toString())));
            caseManage.setUnitTension6(BigDecimal.valueOf(Double.parseDouble(obj.get("Unit_Tension6").toString())));

            List<Map<String, String>> mapList1 = new ArrayList<>();
            HmiBaseRequest hmiBaseRequest1 = new HmiBaseRequest();
            map.put("res_EN_WIDTH", caseManage.getExWidth());
            map.put("Deform_Rate1", caseManage.getDeformRate1());
            map.put("Deform_Rate2", caseManage.getDeformRate2());
            map.put("Deform_Rate3", caseManage.getDeformRate3());
            map.put("Deform_Rate4", caseManage.getDeformRate4());
            map.put("Deform_Rate5", caseManage.getDeformRate5());
            map.put("Roll_Num1", caseManage.getNr1());
            map.put("Roll_Num2", caseManage.getNr2());
            map.put("Roll_Num3", caseManage.getNr3());
            map.put("Roll_Num4", caseManage.getNr4());
            map.put("Roll_Num5", caseManage.getNr5());
            map.put("Unit_Tension1", caseManage.getUnitTension1());
            map.put("Unit_Tension2", caseManage.getUnitTension2());
            map.put("Unit_Tension3", caseManage.getUnitTension3());
            map.put("Unit_Tension4", caseManage.getUnitTension4());
            map.put("Unit_Tension5", caseManage.getUnitTension5());
            map.put("Unit_Tension6", caseManage.getUnitTension6());
            map.put("Diameter1", caseManage.getWrDia1());
            map.put("Diameter2", caseManage.getWrDia2());
            map.put("Diameter3", caseManage.getWrDia3());
            map.put("Diameter4", caseManage.getWrDia4());
            map.put("Diameter5", caseManage.getWrDia5());
            map.put("L", caseManage.getL());
            map.put("M", caseManage.getM());
            map.put("N", caseManage.getN());
            map.put("myu10", 0.026);
            map.put("myu11", 3.5);
            map.put("myu12", 100);
            map.put("myu13", 0);
            map.put("myu14", 1.1);
            map.put("myu15", 0.002);
            map.put("myu20", 0.002);
            map.put("myu21", 4.8);
            map.put("myu22", 80);
            map.put("myu23", 0);
            map.put("myu24", 1.2);
            map.put("myu25", 0.004);
            map.put("myu30", 0.002);
            map.put("myu31", 4.8);
            map.put("myu32", 80);
            map.put("myu33", 0);
            map.put("myu34", 1.2);
            map.put("myu35", 0.004);
            map.put("myu40", 0.002);
            map.put("myu41", 4.8);
            map.put("myu42", 80);
            map.put("myu43", 0);
            map.put("myu44", 1.1);
            map.put("myu45", 0.002);
            map.put("myu50", 0.15);
            map.put("myu51", 5);
            map.put("myu52", 150);
            map.put("myu53", 0);
            map.put("myu54", 1);
            map.put("myu55", 0);
            mapList1.add(map);
            Body body1 = new Body();
            body1.setMsgId(2028);
            List<List<Map<String, String>>> list2 = new ArrayList<>();
            list2.add(mapList);
            body1.setParams(list2);
            hmiBaseRequest1.setBody(body1);
            String json1 = JSON.toJSONString(hmiBaseRequest1);
            String respStr1 = httpClientService.postJsonRequest(hmi, json1, false, "");
            HmiBaseResponse responseDto1 = JSONObject.parseObject(respStr1, HmiBaseResponse.class);
            if (responseDto1.getCode() == 200) {
                if (responseDto1.getResults().size() == 0) {
                    return;
                }
                Object object1 = responseDto1.getResults().get(0);
                JSONArray picArray1 = JSONArray.parseArray(object1.toString());
                JSONObject obj1 = picArray1.getJSONObject(0);

                caseManage.setThk1(BigDecimal.valueOf(Double.parseDouble(obj1.get("EXITTHK1").toString())));
                caseManage.setThk2(BigDecimal.valueOf(Double.parseDouble(obj1.get("EXITTHK2").toString())));
                caseManage.setThk3(BigDecimal.valueOf(Double.parseDouble(obj1.get("EXITTHK3").toString())));
                caseManage.setThk4(BigDecimal.valueOf(Double.parseDouble(obj1.get("EXITTHK4").toString())));
                caseManage.setThk5(BigDecimal.valueOf(Double.parseDouble(obj1.get("EXITTHK5").toString())));

                caseManage.setSpeed1(BigDecimal.valueOf(Double.parseDouble(obj1.get("SPEED1").toString())));
                caseManage.setSpeed2(BigDecimal.valueOf(Double.parseDouble(obj1.get("SPEED2").toString())));
                caseManage.setSpeed3(BigDecimal.valueOf(Double.parseDouble(obj1.get("SPEED3").toString())));
                caseManage.setSpeed4(BigDecimal.valueOf(Double.parseDouble(obj1.get("SPEED4").toString())));
                caseManage.setSpeed5(BigDecimal.valueOf(Double.parseDouble(obj1.get("SPEED5").toString())));

                caseManage.setTension1(BigDecimal.valueOf(Double.parseDouble(obj1.get("TENSION1").toString())));
                caseManage.setTension2(BigDecimal.valueOf(Double.parseDouble(obj1.get("TENSION2").toString())));
                caseManage.setTension3(BigDecimal.valueOf(Double.parseDouble(obj1.get("TENSION3").toString())));
                caseManage.setTension4(BigDecimal.valueOf(Double.parseDouble(obj1.get("TENSION4").toString())));
                caseManage.setTension5(BigDecimal.valueOf(Double.parseDouble(obj1.get("TENSION5").toString())));

                caseManage.setRollForce1(BigDecimal.valueOf(Double.parseDouble(obj1.get("ROLLFORCE1").toString())));
                caseManage.setRollForce2(BigDecimal.valueOf(Double.parseDouble(obj1.get("ROLLFORCE2").toString())));
                caseManage.setRollForce3(BigDecimal.valueOf(Double.parseDouble(obj1.get("ROLLFORCE3").toString())));
                caseManage.setRollForce4(BigDecimal.valueOf(Double.parseDouble(obj1.get("ROLLFORCE4").toString())));
                caseManage.setRollForce5(BigDecimal.valueOf(Double.parseDouble(obj1.get("ROLLFORCE5").toString())));

                caseManage.setGap1(BigDecimal.valueOf(Double.parseDouble(obj1.get("GAP1").toString())));
                caseManage.setGap2(BigDecimal.valueOf(Double.parseDouble(obj1.get("GAP2").toString())));
                caseManage.setGap3(BigDecimal.valueOf(Double.parseDouble(obj1.get("GAP3").toString())));
                caseManage.setGap4(BigDecimal.valueOf(Double.parseDouble(obj1.get("GAP4").toString())));
                caseManage.setGap5(BigDecimal.valueOf(Double.parseDouble(obj1.get("GAP5").toString())));

                caseManage.setForslip1(BigDecimal.valueOf(Double.parseDouble(obj1.get("FORSLIP1").toString())));
                caseManage.setForslip2(BigDecimal.valueOf(Double.parseDouble(obj1.get("FORSLIP2").toString())));
                caseManage.setForslip3(BigDecimal.valueOf(Double.parseDouble(obj1.get("FORSLIP3").toString())));
                caseManage.setForslip4(BigDecimal.valueOf(Double.parseDouble(obj1.get("FORSLIP4").toString())));
                caseManage.setForslip5(BigDecimal.valueOf(Double.parseDouble(obj1.get("FORSLIP5").toString())));

                caseManage.setPower1(BigDecimal.valueOf(Double.parseDouble(obj1.get("MOTORPOWER1").toString())));
                caseManage.setPower2(BigDecimal.valueOf(Double.parseDouble(obj1.get("MOTORPOWER2").toString())));
                caseManage.setPower3(BigDecimal.valueOf(Double.parseDouble(obj1.get("MOTORPOWER3").toString())));
                caseManage.setPower4(BigDecimal.valueOf(Double.parseDouble(obj1.get("MOTORPOWER4").toString())));
                caseManage.setPower5(BigDecimal.valueOf(Double.parseDouble(obj1.get("MOTORPOWER5").toString())));
                caseManage.setStatus(1);
                caseManage.setRecCreateTime(new Date());

                optimizeDao.updateCase(caseManage);

            } else {
                throw new RuntimeException("计算仿真结果为空！");
            }
        } else {
            throw new RuntimeException("计算轧制规程参数为空！");
        }
    }

    @Override
    public ModelAndView exportTemplate(Map<String, Object> map) {

        ModelMap model = new ModelMap();
        XSSFWorkbook workbook = new XSSFWorkbook();
        String titles[] = new String[]{"任务名称", "产品牌号", "屈服强度", "入口厚度", "成品厚度", "宽度", "L", "M", "N"};
        XSSFSheet sheet = workbook.createSheet("批量导入");
        Row row = sheet.createRow(0);
        for (int i = 0; i < titles.length; i++) {
            String str = titles[i];
            Cell cell = row.createCell(i);
            cell.setCellValue(str);
        }

        CellStyle textStyle = workbook.createCellStyle();
        DataFormat format = workbook.createDataFormat();
        textStyle.setDataFormat(format.getFormat("@"));
        for (int i = 0; i <= 8; i++) {
            sheet.setColumnWidth(i, 5000);
            sheet.setDefaultColumnStyle(i, textStyle);
        }

        sheet.setColumnWidth(0, 5000);
        model.addAttribute("workBook", workbook);
        model.addAttribute("fileName", "轧制仿真计算批量导入模板" + ".xlsx");
        return new ModelAndView(new CExportExcelView(), model);


    }


    @Override
    public ResultData simulationReport(SimulationReportRequest request) {
        try {
            List<SimulationReportResp> simulationReport = optimizeDao.simulationReport(request);
            return ResultData.ResultDataSuccess(simulationReport);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }

    @Override
    public ResultData classStatistics(ClassStatisticsReq request) {
        try {
            //Page<?> page = new Page<>(request.getPageIndex(), request.getPageSize());
            List<ClassStatisticsResp> classStatistics = optimizeDao.classStatistics(request);
            //Integer count = optimizeDao.classStatisticsCount(request);
            return ResultData.ResultDataSuccess(classStatistics, classStatistics.size());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }

    @Override
    public ResultData findByYpClass(ClassStatisticsReq request) {
        try {
            //Page<?> page = new Page<>(request.getPageIndex(), request.getPageSize());
            List<ClassStatisticsResp> classStatistics = optimizeDao.findByYpClass(request);
            //Integer count = optimizeDao.findByYpClassCount(request);
            return ResultData.ResultDataSuccess(classStatistics, classStatistics.size());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }


    @Override
    public ModelAndView classStatisticsExport(Map<String, Object> map) {

        ModelMap model = new ModelMap();
        XSSFWorkbook workbook = new XSSFWorkbook();
        String titles[] = new String[]{"钢种分档", "宽度分档", "厚度分档", "描述", "S1", "S2", "S3", "S4", "S5", "ZP1", "ZP2", "ZP3", "ZP4", "ZP5"};
        XSSFSheet sheet = workbook.createSheet("导出");
        Row row = sheet.createRow(0);
        for (int i = 0; i < titles.length; i++) {
            String str = titles[i];
            Cell cell = row.createCell(i);
            cell.setCellValue(str);
        }

        CellStyle textStyle = workbook.createCellStyle();
        DataFormat format = workbook.createDataFormat();
        textStyle.setDataFormat(format.getFormat("@"));
        for (int i = 0; i <= 13; i++) {
            sheet.setColumnWidth(i, 5000);
            sheet.setDefaultColumnStyle(i, textStyle);
        }

        try {
            generHideSheet(workbook, sheet, map, titles);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        sheet.setColumnWidth(0, 5000);
        model.addAttribute("workBook", workbook);
        model.addAttribute("fileName", "轧制仿真模型精度报表" + ".xlsx");
        return new ModelAndView(new CExportExcelView(), model);


    }


    /**
     * excel写入数据
     */
    public void generHideSheet(XSSFWorkbook workbook, XSSFSheet sheet, Map<String, Object> map, String[] titles) throws Exception {
        ClassStatisticsReq request = new ClassStatisticsReq();
        request.setYpClass(Integer.parseInt(map.get("ypClass").toString()));
        request.setWidthClass(Integer.parseInt(map.get("widthClass").toString()));
        request.setThicknessClass(Integer.parseInt(map.get("thicknessClass").toString()));

        if (!map.get("startDate").toString().equals("")) {
            request.setStartDate(map.get("startDate").toString());
        }
        if (!map.get("endDate").toString().equals("")) {
            request.setEndDate(map.get("endDate").toString());
        }
        List<ClassStatisticsResp> classStatistics = optimizeDao.classStatistics(request);

        for (int j = 1; j <= classStatistics.size(); j++) {
            int n = j - 1;
            for (int i = 1; i < 5; i++) {
                XSSFRow row = sheet.createRow(n * 4 + i);
                ClassStatisticsResp classStatisticsResp = classStatistics.get(n);

                for (int k = 0; k < titles.length; k++) {//列数

                    XSSFCell cell = row.createCell(k);
                    Object valueObject = null;
                    String value = null;
                    if (titles[k].equals("钢种分档")) {
                        valueObject = classStatisticsResp.getYpIndex();
                    } else if (titles[k].equals("宽度分档")) {
                        valueObject = classStatisticsResp.getWidIndex();
                    } else if (titles[k].equals("厚度分档")) {
                        valueObject = classStatisticsResp.getThkIndex();
                    } else if (titles[k].equals("描述")) {
                        if (i == 1) {
                            valueObject = "<-10%卷数";
                        } else if (i == 2) {
                            valueObject = ">+10%卷数";
                        } else if (i == 3) {
                            valueObject = "总卷数";
                        } else {
                            valueObject = "[-10%,10%]占比";
                        }
                    } else if (titles[k].equals("S1")) {
                        if (i == 1) {
                            valueObject = classStatisticsResp.getDiffxf1();
                        } else if (i == 2) {
                            valueObject = classStatisticsResp.getDiffd1();
                        } else if (i == 3) {
                            valueObject = classStatisticsResp.getDifft1();
                        } else {
                            valueObject = classStatisticsResp.getDifft1() == 0 ? "0" : String.format("%.2f", (double) classStatisticsResp.getDiffx1() / classStatisticsResp.getDifft1() * 100) + "%";
                        }
                    } else if (titles[k].equals("S2")) {
                        if (i == 1) {
                            valueObject = classStatisticsResp.getDiffxf2();
                        } else if (i == 2) {
                            valueObject = classStatisticsResp.getDiffd2();
                        } else if (i == 3) {
                            valueObject = classStatisticsResp.getDifft2();
                        } else {
                            valueObject = classStatisticsResp.getDifft2() == 0 ? "0" : String.format("%.2f", (double) classStatisticsResp.getDiffx2() / classStatisticsResp.getDifft2() * 100) + "%";
                        }
                    } else if (titles[k].equals("S3")) {
                        if (i == 1) {
                            valueObject = classStatisticsResp.getDiffxf3();
                        } else if (i == 2) {
                            valueObject = classStatisticsResp.getDiffd3();
                        } else if (i == 3) {
                            valueObject = classStatisticsResp.getDifft3();
                        } else {
                            valueObject = classStatisticsResp.getDifft3() == 0 ? "0" : String.format("%.2f", (double) classStatisticsResp.getDiffx3() / classStatisticsResp.getDifft3() * 100) + "%";
                        }
                    } else if (titles[k].equals("S4")) {
                        if (i == 1) {
                            valueObject = classStatisticsResp.getDiffxf4();
                        } else if (i == 2) {
                            valueObject = classStatisticsResp.getDiffd4();
                        } else if (i == 3) {
                            valueObject = classStatisticsResp.getDifft4();
                        } else {
                            valueObject = classStatisticsResp.getDifft4() == 0 ? "0" : String.format("%.2f", (double) classStatisticsResp.getDiffx4() / classStatisticsResp.getDifft4() * 100) + "%";
                        }
                    } else if (titles[k].equals("S5")) {
                        if (i == 1) {
                            valueObject = classStatisticsResp.getDiffxf5();
                        } else if (i == 2) {
                            valueObject = classStatisticsResp.getDiffd5();
                        } else if (i == 3) {
                            valueObject = classStatisticsResp.getDifft5();
                        } else {
                            valueObject = classStatisticsResp.getDifft5() == 0 ? "0" : String.format("%.2f", (double) classStatisticsResp.getDiffx5() / classStatisticsResp.getDifft5() * 100) + "%";
                        }
                    } else if (titles[k].equals("ZP1")) {
                        if (i == 1) {
                            valueObject = "0";
                        } else if (i == 2) {
                            valueObject = classStatisticsResp.getZpd1();
                        } else if (i == 3) {
                            valueObject = classStatisticsResp.getZpt1();
                        } else {
                            valueObject = classStatisticsResp.getZpx1() == 0 ? "0" : String.format("%.2f", (double) classStatisticsResp.getZpx1() / classStatisticsResp.getZpt1() * 100) + "%";
                        }
                    } else if (titles[k].equals("ZP2")) {
                        if (i == 1) {
                            valueObject = "0";
                        } else if (i == 2) {
                            valueObject = classStatisticsResp.getZpd2();
                        } else if (i == 3) {
                            valueObject = classStatisticsResp.getZpt2();
                        } else {
                            valueObject = classStatisticsResp.getZpx2() == 0 ? "0" : String.format("%.2f", (double) classStatisticsResp.getZpx2() / classStatisticsResp.getZpt2() * 100) + "%";
                        }

                    } else if (titles[k].equals("ZP3")) {
                        if (i == 1) {
                            valueObject = "0";
                        } else if (i == 2) {
                            valueObject = classStatisticsResp.getZpd3();
                        } else if (i == 3) {
                            valueObject = classStatisticsResp.getZpt3();
                        } else {
                            valueObject = classStatisticsResp.getZpx3() == 0 ? "0" : String.format("%.2f", (double) classStatisticsResp.getZpx3() / classStatisticsResp.getZpt3() * 100) + "%";
                        }
                    } else if (titles[k].equals("ZP4")) {
                        if (i == 1) {
                            valueObject = "0";
                        } else if (i == 2) {
                            valueObject = classStatisticsResp.getZpd4();
                        } else if (i == 3) {
                            valueObject = classStatisticsResp.getZpt4();
                        } else {
                            valueObject = classStatisticsResp.getZpx4() == 0 ? "0" : String.format("%.2f", (double) classStatisticsResp.getZpx4() / classStatisticsResp.getZpt4() * 100) + "%";
                        }
                    } else if (titles[k].equals("ZP5")) {
                        if (i == 1) {
                            valueObject = "0";
                        } else if (i == 2) {
                            valueObject = classStatisticsResp.getZpd5();
                        } else if (i == 3) {
                            valueObject = classStatisticsResp.getZpt5();
                        } else {
                            valueObject = classStatisticsResp.getZpx5() == 0 ? "0" : String.format("%.2f", (double) classStatisticsResp.getZpx5() / classStatisticsResp.getZpt5() * 100) + "%";
                        }
                    }
                    if (valueObject instanceof String) {
                        //取出的数据是字符串直接赋值
                        value = (String) valueObject;
                    } else if (valueObject instanceof Integer) {
                        //取出的数据是Integer
                        value = String.valueOf(((Integer) (valueObject)));
                    } else if (valueObject instanceof BigDecimal) {
                        //取出的数据是BigDecimal
                        value = String.valueOf(((BigDecimal) (valueObject)).floatValue());
                    } else if (valueObject instanceof Date) {
                        value = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(valueObject);
                    } else {
                        if (valueObject == null) {
                            valueObject = "";
                        }
                        value = valueObject.toString();
                    }
                    cell.setCellValue(value);
                }
            }
        }


    }


    @Override
    public ModelAndView acidRollingProductExport(Map<String, Object> map) {

        ModelMap model = new ModelMap();
        XSSFWorkbook workbook = new XSSFWorkbook();
        String titles[] = new String[]{"钢卷号", "钢种", "入口宽度", "入口厚度", "出口宽度", "出口厚度", "记录时间", "屈服强度分档", "宽度分档", "厚度分档", "S1轧制力(计算值)", "S1轧制力(设定值)", "S1轧制力(实际值)", "S2轧制力(计算值)", "S2轧制力(设定值)", "S2轧制力(实际值)", "S3轧制力(计算值)", "S3轧制力(设定值)", "S3轧制力(实际值)", "S4轧制力(计算值)", "S4轧制力(设定值)", "S4轧制力(实际值)", "S5轧制力(计算值)", "S5轧制力(设定值)", "S5轧制力(实际值)", "S1入口张力", "S1-S2张力", "S2-S3张力", "S3-S4张力", "S4-S5张力", "S5出口张力", "S1速度", "S2速度", "S3速度", "S4速度", "S5速度", "S1厚度", "S2厚度", "S3厚度", "S4厚度", "S5厚度", "S1功率", "S2功率", "S3功率", "S4功率", "S5功率", "S1工作辊弯辊力", "S2工作辊弯辊力", "S3工作辊弯辊力", "S4工作辊弯辊力", "S5工作辊弯辊力", "S1中间辊弯辊力", "S2中间辊弯辊力", "S3中间辊弯辊力", "S4中间辊弯辊力", "S5中间辊弯辊力", "ZP1", "ZP2", "ZP3", "ZP4"};
        XSSFSheet sheet = workbook.createSheet("导出");
        Row row = sheet.createRow(0);
        for (int i = 0; i < titles.length; i++) {
            String str = titles[i];
            Cell cell = row.createCell(i);
            cell.setCellValue(str);
        }

        CellStyle textStyle = workbook.createCellStyle();
        DataFormat format = workbook.createDataFormat();
        textStyle.setDataFormat(format.getFormat("@"));
        for (int i = 0; i <= 59; i++) {
            sheet.setColumnWidth(i, 5000);
            sheet.setDefaultColumnStyle(i, textStyle);
        }

        try {
            acidRollingProductGenerHideSheet(workbook, sheet, map, titles);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        sheet.setColumnWidth(0, 5000);
        model.addAttribute("workBook", workbook);
        model.addAttribute("fileName", "酸轧生产实绩报表" + ".xlsx");
        return new ModelAndView(new CExportExcelView(), model);
    }

    /**
     * excel写入数据
     */
    public void acidRollingProductGenerHideSheet(XSSFWorkbook workbook, XSSFSheet sheet, Map<String, Object> map, String[] titles) throws Exception {
        AcidRollingProductRequest request = new AcidRollingProductRequest();
        request.setVolumeNum(map.get("volumeNum").toString());
        request.setSteelGrade(map.get("steelGrade").toString());
        request.setInletWidth(map.get("inletWidth").toString());
        request.setEntryThickness(map.get("entryThickness").toString());

        if (!StringUtils.isEmpty(map.get("startDate").toString())) {
            request.setStartDate(map.get("startDate").toString());
        }
        if (!StringUtils.isEmpty(map.get("endDate").toString())) {
            request.setEndDate(map.get("endDate").toString());
        }

        List<ParamOptimizeEntity> list = optimizeDao.findExcelMapByCondition(request);

        for (int j = 1; j <= list.size(); j++) {
            int n = j - 1;
            XSSFRow row = sheet.createRow(j);
            ParamOptimizeEntity paramOptimizeEntity = list.get(n);
            for (int k = 0; k < titles.length; k++) {//列数

                XSSFCell cell = row.createCell(k);
                Object valueObject = null;
                String value = null;
                if (titles[k].equals("钢卷号")) {
                    valueObject = paramOptimizeEntity.getCoilNo();
                } else if (titles[k].equals("钢种")) {
                    valueObject = paramOptimizeEntity.getSteelGrade();
                } else if (titles[k].equals("入口宽度")) {
                    valueObject = paramOptimizeEntity.getEnWidth();
                } else if (titles[k].equals("入口厚度")) {
                    valueObject = paramOptimizeEntity.getEnThick();
                } else if (titles[k].equals("出口宽度")) {
                    valueObject = paramOptimizeEntity.getExWidth();
                } else if (titles[k].equals("出口厚度")) {
                    valueObject = paramOptimizeEntity.getExThick();
                } else if (titles[k].equals("记录时间")) {
                    valueObject = paramOptimizeEntity.getRecCreateTime();
                } else if (titles[k].equals("屈服强度分档")) {
                    valueObject = paramOptimizeEntity.getYpIndex();
                } else if (titles[k].equals("宽度分档")) {
                    valueObject = paramOptimizeEntity.getWidIndex();
                } else if (titles[k].equals("厚度分档")) {
                    valueObject = paramOptimizeEntity.getThkIndex();
                } else if (titles[k].equals("S1轧制力(计算值)")) {
                    valueObject = paramOptimizeEntity.getJszRollForce1() == null ? paramOptimizeEntity.getJszRollForce1() : paramOptimizeEntity.getJszRollForce1().multiply(new BigDecimal("10")).setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S1轧制力(设定值)")) {
                    valueObject = paramOptimizeEntity.getRollingForceSet1().setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S1轧制力(实际值)")) {
                    valueObject = paramOptimizeEntity.getRollForce1().setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S2轧制力(计算值)")) {
                    valueObject = paramOptimizeEntity.getJszRollForce2() == null ? paramOptimizeEntity.getJszRollForce2() : paramOptimizeEntity.getJszRollForce2().multiply(new BigDecimal("10")).setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S2轧制力(设定值)")) {
                    valueObject = paramOptimizeEntity.getRollingForceSet2().setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S2轧制力(实际值)")) {
                    valueObject = paramOptimizeEntity.getRollForce2().setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S3轧制力(计算值)")) {
                    valueObject = paramOptimizeEntity.getJszRollForce3() == null ? paramOptimizeEntity.getJszRollForce3() : paramOptimizeEntity.getJszRollForce3().multiply(new BigDecimal("10")).setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S3轧制力(设定值)")) {
                    valueObject = paramOptimizeEntity.getRollingForceSet3().setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S3轧制力(实际值)")) {
                    valueObject = paramOptimizeEntity.getRollForce3().setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S4轧制力(计算值)")) {
                    valueObject = paramOptimizeEntity.getJszRollForce4() == null ? paramOptimizeEntity.getJszRollForce4() : paramOptimizeEntity.getJszRollForce4().multiply(new BigDecimal("10")).setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S4轧制力(设定值)")) {
                    valueObject = paramOptimizeEntity.getRollingForceSet4().setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S4轧制力(实际值)")) {
                    valueObject = paramOptimizeEntity.getRollForce4().setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S5轧制力(计算值)")) {
                    valueObject = paramOptimizeEntity.getJszRollForce5() == null ? paramOptimizeEntity.getJszRollForce5() : paramOptimizeEntity.getJszRollForce5().multiply(new BigDecimal("10")).setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S5轧制力(设定值)")) {
                    valueObject = paramOptimizeEntity.getRollingForceSet5().setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S5轧制力(实际值)")) {
                    valueObject = paramOptimizeEntity.getRollForce5().setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S1入口张力")) {
                    valueObject = paramOptimizeEntity.getTension1().multiply(new BigDecimal("1000"));
                } else if (titles[k].equals("S1-S2张力")) {
                    valueObject = paramOptimizeEntity.getTension2().multiply(new BigDecimal("1000"));
                } else if (titles[k].equals("S2-S3张力")) {
                    valueObject = paramOptimizeEntity.getTension3().multiply(new BigDecimal("1000"));
                } else if (titles[k].equals("S3-S4张力")) {
                    valueObject = paramOptimizeEntity.getTension4().multiply(new BigDecimal("1000"));
                } else if (titles[k].equals("S4-S5张力")) {
                    valueObject = paramOptimizeEntity.getTension5().multiply(new BigDecimal("1000"));
                } else if (titles[k].equals("S5出口张力")) {
                    valueObject = paramOptimizeEntity.getTension6().multiply(new BigDecimal("1000"));
                } else if (titles[k].equals("S1速度")) {
                    valueObject = paramOptimizeEntity.getSpeed1().setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S2速度")) {
                    valueObject = paramOptimizeEntity.getSpeed2().setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S3速度")) {
                    valueObject = paramOptimizeEntity.getSpeed3().setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S4速度")) {
                    valueObject = paramOptimizeEntity.getSpeed4().setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S5速度")) {
                    valueObject = paramOptimizeEntity.getSpeed5().setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S1厚度")) {
                    valueObject = paramOptimizeEntity.getThk1();
                } else if (titles[k].equals("S2厚度")) {
                    valueObject = paramOptimizeEntity.getThk2();
                } else if (titles[k].equals("S3厚度")) {
                    valueObject = paramOptimizeEntity.getThk3();
                } else if (titles[k].equals("S4厚度")) {
                    valueObject = paramOptimizeEntity.getThk4();
                } else if (titles[k].equals("S5厚度")) {
                    valueObject = paramOptimizeEntity.getThk5();
                } else if (titles[k].equals("S1功率")) {
                    valueObject = paramOptimizeEntity.getPOWER1().setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S2功率")) {
                    valueObject = paramOptimizeEntity.getPOWER2().setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S3功率")) {
                    valueObject = paramOptimizeEntity.getPOWER3().setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S4功率")) {
                    valueObject = paramOptimizeEntity.getPOWER4().setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S5功率")) {
                    valueObject = paramOptimizeEntity.getPOWER5().setScale(0, RoundingMode.HALF_UP);
                } else if (titles[k].equals("S1工作辊弯辊力")) {
                    valueObject = paramOptimizeEntity.getWBEND1();
                } else if (titles[k].equals("S2工作辊弯辊力")) {
                    valueObject = paramOptimizeEntity.getWBEND2();
                } else if (titles[k].equals("S3工作辊弯辊力")) {
                    valueObject = paramOptimizeEntity.getWBEND3();
                } else if (titles[k].equals("S4工作辊弯辊力")) {
                    valueObject = paramOptimizeEntity.getWBEND4();
                } else if (titles[k].equals("S5工作辊弯辊力")) {
                    valueObject = paramOptimizeEntity.getWBEND5();
                } else if (titles[k].equals("S1中间辊弯辊力")) {
                    valueObject = paramOptimizeEntity.getIMBEND1();
                } else if (titles[k].equals("S2中间辊弯辊力")) {
                    valueObject = "";
                } else if (titles[k].equals("S3中间辊弯辊力")) {
                    valueObject = "";
                } else if (titles[k].equals("S4中间辊弯辊力")) {
                    valueObject = "";
                } else if (titles[k].equals("S5中间辊弯辊力")) {
                    valueObject = paramOptimizeEntity.getIMBEND5();
                } else if (titles[k].equals("ZP1")) {
                    valueObject = paramOptimizeEntity.getZp1();
                } else if (titles[k].equals("ZP2")) {
                    valueObject = paramOptimizeEntity.getZp2();
                } else if (titles[k].equals("ZP3")) {
                    valueObject = paramOptimizeEntity.getZp3();
                } else if (titles[k].equals("ZP4")) {
                    valueObject = paramOptimizeEntity.getZp4();
                }
                if (valueObject instanceof String) {
                    //取出的数据是字符串直接赋值
                    value = (String) valueObject;
                } else if (valueObject instanceof Integer) {
                    //取出的数据是Integer
                    value = String.valueOf(((Integer) (valueObject)));
                } else if (valueObject instanceof BigDecimal) {
                    //取出的数据是BigDecimal
                    value = String.valueOf(((BigDecimal) (valueObject)).floatValue());
                } else if (valueObject instanceof Date) {
                    value = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(valueObject);
                } else {
                    if (valueObject == null) {
                        valueObject = "";
                    }
                    value = valueObject.toString();
                }
                cell.setCellValue(value);
            }
        }
    }

    @Override
    public void loadDistributionExport(LoadDistributionRequest req, HttpServletResponse response) {
        List<LoadDistributionEntity> dbList = optimizeDao.findLoadDistributionMapByCondition(req);
        List<ExcelLoadDistributionSchedule> list = new ArrayList<>();
        for (LoadDistributionEntity obj : dbList) {
            ExcelLoadDistributionSchedule excel = new ExcelLoadDistributionSchedule();
            BeanUtils.copyProperties(obj, excel);
            BigDecimal totalReductionRate = excel.getS1Light().add(excel.getS2Light()).add(excel.getS3Light()).add(excel.getS4Light()).add(excel.getS5Light());
            BigDecimal totalReductionRateNew = excel.getS1New().add(excel.getS2New()).add(excel.getS3New()).add(excel.getS4New()).add(excel.getS5New());
            excel.setS1LightPer(excel.getS1Light().divide(totalReductionRate, 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
            excel.setS2LightPer(excel.getS2Light().divide(totalReductionRate, 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
            excel.setS3LightPer(excel.getS3Light().divide(totalReductionRate, 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
            excel.setS4LightPer(excel.getS4Light().divide(totalReductionRate, 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
            excel.setS5LightPer(excel.getS5Light().divide(totalReductionRate, 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));

            list.add(excel);
        }

        try {
            Map<String, Object> dataMap = new HashMap();
            dataMap.put("data", list);
            File file = ResourceUtils.getFile("classpath:exceltemplate\\负荷分配优化.xlsx");
            TemplateExportParams templ = new TemplateExportParams(file.getPath(), false);
            Workbook workbook = ExcelExportUtil.exportExcel(templ, dataMap);
            ExcelUtilsPOI.exportExcel("负荷分配优化", response, workbook);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResultData<OptimizeDistributeRes> distributionData() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_MONTH, -30);
        String beginDate = DateUtil.getDateToString(cal.getTime(),"yyyyMMdd");
        List<OptimizeDistributeRes> resList = optimizeDao.distributionData(beginDate);

        return ResultData.ResultDataSuccess(resList, resList.size());
    }
}
